﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FullThrustLogic;
using FullThrustLogic.ShipComponent;

namespace LogicTest.Forms
{
    public partial class AssignMovementWizard : Form
    {

        public Ship TargetShip;


        public AssignMovementWizard(Ship targetShip)
        {
            TargetShip = targetShip;
            InitializeComponent();
        }

        private void AssignMovementWizard_Load(object sender, EventArgs e)
        {
            txtTotalVelocity.Text = TargetShip.Velocity.ToString();
            txtHeading.Text = TargetShip.Heading.ToString();

            TargetShip.ExecuteMovementOrder(false);

            txtNewTotalVelocity.Text = TargetShip.VelocityAfterMoveOrder.ToString();
            txtNewHeading.Text = TargetShip.HeadingAfterMoveOrder.ToString();
            
            if(TargetShip.CurrentMovementOrder == null)
            {
                nudTurnThrust.Value = 0;
                nudChangeVelocity.Value = 0;
                cbDirection.Items.Add(Direction.None);

                cbDirection.SelectedIndex = 0;
            }
            else
            {
                nudChangeVelocity.ValueChanged -= new EventHandler(nudChangeVelocity_ValueChanged);
                nudTurnThrust.ValueChanged -= new EventHandler(nudTurnThrust_ValueChanged);
                
                nudTurnThrust.Value = TargetShip.CurrentMovementOrder.TurnThrust;
                nudChangeVelocity.Value = TargetShip.CurrentMovementOrder.VelocityAdjustment;

                nudChangeVelocity.Value = TargetShip.CurrentMovementOrder.VelocityAdjustment;

                if (nudTurnThrust.Value == 0)
                {
                    cbDirection.Items.Clear();
                    cbDirection.Items.Add(Direction.None);
                    cbDirection.SelectedItem = TargetShip.CurrentMovementOrder.Direction;
                    cbDirection.Enabled = false;
                }
                else
                {
                    cbDirection.Items.Clear();
                    cbDirection.Items.Add(Direction.Portside);
                    cbDirection.Items.Add(Direction.Starboard);
                    cbDirection.SelectedItem = TargetShip.CurrentMovementOrder.Direction;
                    cbDirection.Enabled = true;
                }


                nudChangeVelocity.ValueChanged += new EventHandler(nudChangeVelocity_ValueChanged);
                nudTurnThrust.ValueChanged += new EventHandler(nudTurnThrust_ValueChanged);

            }
            
        }

        private void nudTurnThrust_ValueChanged(object sender, EventArgs e)
        {
            if (nudTurnThrust.Value < 0)
            {
                nudChangeVelocity.ValueChanged -= new EventHandler(nudChangeVelocity_ValueChanged);
                nudTurnThrust.ValueChanged -= new EventHandler(nudTurnThrust_ValueChanged);

                nudTurnThrust.Value = 0;

                nudChangeVelocity.ValueChanged += new EventHandler(nudChangeVelocity_ValueChanged);
                nudTurnThrust.ValueChanged += new EventHandler(nudTurnThrust_ValueChanged);

            }

            if (nudTurnThrust.Value == 0)
            {
                cbDirection.Items.Clear();
                cbDirection.Items.Add(Direction.None);
                cbDirection.SelectedIndex = 0;
                cbDirection.Enabled = false;
            }
            else if (cbDirection.Items.Count == 1)
            {
                cbDirection.Items.Clear();
                cbDirection.Items.Add(Direction.Portside);
                cbDirection.Items.Add(Direction.Starboard);
                cbDirection.SelectedIndex = 0;
                cbDirection.Enabled = true;
            }

            UpdateAvailableThrust();
        }

        private void nudChangeVelocity_ValueChanged(object sender, EventArgs e)
        {
         

            UpdateAvailableThrust();

           
        }

        private void UpdateAvailableThrust()
        {
            if (TargetShip != null)
            {
                int maxThrust = 0;

                nudChangeVelocity.ValueChanged -= new EventHandler(nudChangeVelocity_ValueChanged);
                nudTurnThrust.ValueChanged -= new EventHandler(nudTurnThrust_ValueChanged);

                foreach (Thrust thrust in TargetShip.Thrusts)
                {
                    if (thrust.Enabled)
                    {
                        maxThrust += thrust.AvailableThrust;
                    }
                }

                nudChangeVelocity.Maximum = maxThrust - nudTurnThrust.Value;
                nudChangeVelocity.Minimum = -maxThrust - nudTurnThrust.Value;

                nudTurnThrust.Maximum = (int)Math.Floor((maxThrust - nudTurnThrust.Value) / 2);
                nudTurnThrust.Minimum = -((int)Math.Floor((maxThrust - nudTurnThrust.Value) / 2));

                nudChangeVelocity.ValueChanged += new EventHandler(nudChangeVelocity_ValueChanged);
                nudTurnThrust.ValueChanged += new EventHandler(nudTurnThrust_ValueChanged);

                TargetShip.CurrentMovementOrder = new MovementOrder()
                {
                    Direction = (Direction)cbDirection.SelectedItem,
                    TurnThrust = (int)nudTurnThrust.Value,
                    VelocityAdjustment = (int)nudChangeVelocity.Value
                };
                
                TargetShip.ExecuteMovementOrder(false);

                txtNewTotalVelocity.Text = TargetShip.VelocityAfterMoveOrder.ToString();
                txtNewHeading.Text = TargetShip.HeadingAfterMoveOrder.ToString();

            }
        }

        private void btnCommit_Click(object sender, EventArgs e)
        {
            TargetShip.CurrentMovementOrder = new MovementOrder()
                                                  {
                                                      Direction = (Direction) cbDirection.SelectedItem,
                                                      TurnThrust = (int) nudTurnThrust.Value,
                                                      VelocityAdjustment = (int) nudChangeVelocity.Value
                                                  };

            TargetShip.ExecuteMovementOrder(false);

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

            TargetShip.CurrentMovementOrder = null;

            this.Close();
        }

    }
}
